# -*- coding: utf-8 -*-
from Reader import Reader
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.family'] = ['KaiTi', 'sans-serif']
matplotlib.rcParams['axes.unicode_minus'] = False
plt.style.use("./journal.mplstyle")
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import MultipleLocator
import config
import numpy as np
import h5py
reader = Reader('data.xlsx')
with h5py.File('model.h5', 'r') as ipt:
    gamma_res_12_5 = ipt['12_5/gamma'][:]
    gamma_res_13_5 = ipt['13_5/gamma'][:]
    beta_res_12_5 = ipt['12_5/beta'][:]
    beta_res_13_5 = ipt['13_5/beta'][:]
    zeta_res_12_5 = ipt['12_5/zeta'][:]
    zeta_res_13_5 = ipt['13_5/zeta'][:]
    alpha_res_12_5 = ipt['12_5/alpha'][:]
    alpha_res_13_5 = ipt['13_5/alpha'][:]
    GP =ipt['GP'][:]
years = np.arange(2016, 2031)
G_P, P = (years-2010)*GP[0,1]+GP[0,0], [*reader.P_GDP['P'].to_numpy()[6:],*(np.ones(10)*reader.P_GDP['P'].to_numpy()[-1])]
alpha = alpha_res_13_5[:,:,0]
zeta = ((years-years[0])*zeta_res_13_5[:,:,1][:,:,np.newaxis]+zeta_res_13_5[:,:,0][:,:,np.newaxis])
beta = (years-years[0])*beta_res_13_5[:,1][:,np.newaxis]+beta_res_13_5[:,0][:,np.newaxis]
gamma = (years-years[0])*gamma_res_13_5[:,1][:,np.newaxis]+gamma_res_13_5[:,0][:,np.newaxis]
predicts = G_P * P * (np.sum(gamma*beta*np.sum(alpha[:,:,np.newaxis]*zeta, axis=1), axis=0))
industryName = config.industryName[np.array([0,2,3,4,5])]
carbons = reader.Carbon[industryName].to_numpy().T
with PdfPages('predict.pdf') as pdf:
    fig, ax = plt.subplots(figsize=(15, 6))
    ax.scatter(np.arange(2010, 2021), np.sum(carbons, axis=0), marker='o')
    ax.plot(years, predicts, color='r', label='十三五预测')
    ax.set_xlabel('年份')
    ax.xaxis.set_major_locator(MultipleLocator(1))
    ax.set_ylabel('碳排放量')
    ax.legend()
    pdf.savefig(fig)